package com.txm.qqserver.service;

import com.txm.qqcommon.Message;
import com.txm.qqcommon.MessageType;
import com.txm.qqcommon.Response;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Set;

public class GroupChat {
    static final String PATH = "/groupChat";

    public static void handleMethods(Response res) {
        if (!res.getPath().equals(PATH)) return;
        try {
            Message msg = (Message) res.getObject();
            switch (msg.getMsgType()) {
                case MessageType.MESSAGE_SENDER_MSG_GROUP:
                    mark(msg);
                    break;
                case MessageType.MESSAGE_CHAT_GROUP:
                    chat(msg);
                    break;
                case MessageType.MESSAGE_CHAT_GROUP_EXIT:
                    chatExit(msg);
                    break;
            }
        } catch (Exception e) {
            System.out.println("GroupChat Exception：" + e);
            e.printStackTrace();
        }
    }

    public static void mark(Message msg) throws IOException {
        ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(msg.getSender());

//      标记
        thread.groupId = msg.getContent();

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(thread.accept.getOutputStream());

        Message message = new Message();
        message.setCurrentLocalDate();
        message.setGetter(msg.getSender());
        message.setContent(msg.getContent());
        message.setMsgType(MessageType.MESSAGE_SENDER_MSG_GROUP);
//        System.out.println("mark:" + message);

        objectOutputStream.writeObject(message);
    }

    public static void chat(Message msg) throws IOException {
        System.out.println("chat:" + msg);
        ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(msg.getSender());

//        System.out.println("thread=>" + thread);

//      需要发送消息的客户端线程
        HashMap<String, Thread> mcts = new HashMap<>();
//      所有客户端线程
        HashMap<String, Thread> mcts1 = ManageClientThreads.getMcts();
        Set<String> strings = mcts1.keySet();
        for (String key : strings) {
            ServerConnectServerThread val_ = (ServerConnectServerThread) mcts1.get(key);
            if (!val_.groupId.isEmpty() && val_.groupId.equals(msg.getGetter()) && val_ != thread) {
                mcts.put(key, val_);
            }
        }
//        System.out.println("mcts=>" + mcts);
        Set<String> strings1 = mcts.keySet();
        for (String key : strings1) {
            ServerConnectServerThread val_ = (ServerConnectServerThread) mcts.get(key);

            ObjectOutputStream toGetter = new ObjectOutputStream(val_.accept.getOutputStream());
            Message message = new Message();
            message.setSender(thread.user.getUsername());
            message.setGetter(msg.getGetter());
            message.setCurrentLocalDate();
            message.setContent(msg.getContent());
            message.setMsgType(MessageType.MESSAGE_CHAT_GROUP);
            toGetter.writeObject(message);
        }
    }

    public static void chatExit(Message msg) throws IOException {
//        System.out.println("chatExit:" + msg);

        ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(msg.getSender());

        thread.groupId = "";
        ObjectOutputStream oos = new ObjectOutputStream(thread.accept.getOutputStream());
        Message message = new Message();
        message.setContent("当前客户端已退出群聊。");
        message.setMsgType(MessageType.MESSAGE_CHAT_GROUP_EXIT);
        oos.writeObject(message);
    }
}
